local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseObject());
local ConnectClass = _G.CommonOHOH.load.Connect();
local game_cfg=ServerOHOH.config.Game

local HttpService=game:GetService("HttpService")

function M.new(self,...)
	return M(self,...);
end;
function M.ctor(self,sender,accepter)
	M.super.ctor(self);
    self.DtorAction=ConnectClass.new(self.id)

    self.id=HttpService:GenerateGUID(true)
    self.sender=sender
    self.accepter=accepter

    self.senderCtrl=sender:GetComponent(_G.CommonOHOH.load.Common_TradeCtrl())
    self.accepterCtrl=accepter:GetComponent(_G.CommonOHOH.load.Common_TradeCtrl())

    self.senderIsReady=false
    self.accepterIsReady=false
    
    local c1=self.senderCtrl.ChangeAction:Connect(function()

        self.accepterIsReady=false
        self.senderIsReady=false
        self.accepterCtrl:UpdateTargetPet(self.senderCtrl:GetTradePet())

      

    end)

    local c2=self.accepterCtrl.ChangeAction:Connect(function()

        self.senderIsReady=false
        self.accepterIsReady=false
        self.senderCtrl:UpdateTargetPet(self.accepterCtrl:GetTradePet())
    end)

    local c3=self.senderCtrl.ReadyAction:Connect(function()


        self.senderIsReady=true
        self.accepterCtrl:TargetReady()
        if self:IsAllReady() then
            self:DealCountdown()
        end
    end)

    local c4=self.accepterCtrl.ReadyAction:Connect(function()



        self.accepterIsReady=true
        self.senderCtrl:TargetReady()
        if self:IsAllReady() then
            self:DealCountdown()
        end

    end)

    local c5=self.accepterCtrl.CancelAction:Connect(function()
        self.senderCtrl:TargetCancel(self.accepter:GetUserId(),self.id)
        self:CancelTrade()
    end)

    local c6=self.senderCtrl.CancelAction:Connect(function()
        self.accepterCtrl:TargetCancel(self.sender:GetUserId(),self.id)
        self:CancelTrade()
    end)

    local d1=self.accepterCtrl.DtorAction:Connect(function()
        self.senderCtrl:TargetCancel(self.accepter:GetUserId(),self.id)
        self:CancelTrade()
    end)

    local d2=self.senderCtrl.DtorAction:Connect(function()
        self.accepterCtrl:TargetCancel(self.sender:GetUserId(),self.id)
        self:CancelTrade()
    end)

    local c7=self:AddProcess(function()
        self:Wait(game_cfg.NoVisileTime)
        if self.Visible then
            self:dtor()
        end
    end)

    
    self.ConnectList={}

    table.insert(self.ConnectList,c1)
    table.insert(self.ConnectList,c2)
    table.insert(self.ConnectList,c3)
    table.insert(self.ConnectList,c4)
    table.insert(self.ConnectList,c5)
    table.insert(self.ConnectList,c6)

    table.insert(self.ConnectList,d1)
    table.insert(self.ConnectList,d2)

    table.insert(self.ConnectList,c7)

end;


function M.IsCanTrade(self)
   return  self.senderCtrl:IsCanTrade(self.accepter) and self.accepterCtrl:IsCanTrade(self.sender)
end

-- 所有人都准备了吗
function M.IsAllReady(self)
    return  self.senderIsReady and self.accepterIsReady
end

-- 交易倒计时
function M.DealCountdown(self)
    self.accepterCtrl:ReadyFinish()
    self.senderCtrl:ReadyFinish()
   local c=self:AddProcess(function()
        self:Wait(game_cfg.TradeTime)
        self.accepterCtrl:Finish(self.senderCtrl:GetTradePet())
        self.senderCtrl:Finish(self.accepterCtrl:GetTradePet())
        self:dtor()
    end)
    table.insert(self.ConnectList,c)
end

-- 取消交易
function M.CancelTrade(self)
    self:dtor()
end

function M.dtor(self)

    if self.ConnectList then
        for _,v in pairs(self.ConnectList) do
            v:Disconnect()
        end
        self.ConnectList=nil
    end
    self.Visible=nil

    self.DtorAction:Action(self.id)
	M.super.dtor(self);

end;

function M.GetId(self)
    return  self.id
end

function M.StartTrade(self)
    self.Visible=true
    self.senderCtrl:StartTrade(self.accepter:GetUserId(),self.id,self.accepterCtrl:GetAllPet())
    self.accepterCtrl:StartTrade(self.sender:GetUserId(),self.id,self.senderCtrl:GetAllPet())
end


return M;
